/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/

#include <stdlib.h>
#include <flgrCoreMalloc.h>
#include <flgrCoreDispatch.h>
#include "flgrDataToolsStaticFifo.h"

/*!
 * \defgroup group_fulguro_datatools_static_fifo Static Size Fifo
 * \brief Basic tools for manipulating Static Size Fifo
 * \ingroup group_fulguro_datatools
 * @{
 */

//////////////////////////////////////////////////////////////////
/*! Init the fifo using size of parameters of an image
 *  @param size_y : row number of the image used to create the fifo
 *  @param size_x : column number of the image used to create the fifo
 *  @returns pointer to FLGR_StaticFifo variable
 */
//////////////////////////////////////////////////////////////////
FLGR_StaticFifo *flgr_static_fifo_init(int size_y, int size_x) {
  FLGR_StaticFifo *fifo=flgr_malloc(sizeof(FLGR_StaticFifo));
  
  FLGR_DECLARE_FUNCTION;
  
  fifo->size=size_y*size_x+1;
  fifo->size_x=size_x;
  fifo->size_y=size_y;
  fifo->row=(int *) flgr_malloc(sizeof(int)*fifo->size);
  fifo->col=(int *) flgr_malloc(sizeof(int)*fifo->size);
  fifo->rdptr=0;
  fifo->wrptr=0;

  return fifo;
}

//////////////////////////////////////////////////////////////////
/*! write pixel coordinate into the fifo
 *  @param fifo : Pointer to the fifo
 *  @param row : row coordinate
 *  @param col : column coordinate
 *  @returns nothing
 */
//////////////////////////////////////////////////////////////////
void flgr_static_fifo_write(FLGR_StaticFifo *fifo, int row, int col) {
  int wr=(fifo->wrptr);
  int isWrite=1;
  
  FLGR_DECLARE_FUNCTION;
  
  if((row>=0) && (row<fifo->size_y))
    fifo->row[wr]=row;
  else 
    isWrite=0;
  
  if((col>=0) && (col<fifo->size_x))
    fifo->col[wr]=col;
  else
    isWrite=0;

  if(isWrite==1) fifo->wrptr=(fifo->wrptr+1) % fifo->size;
}


//////////////////////////////////////////////////////////////////
/*! read one pixel coordinate into the fifo
 *  @param fifo : Pointer to the fifo
 *  @param row : row pointer coordinate
 *  @param col : column pointer coordinate
 *  @returns nothing
 */
//////////////////////////////////////////////////////////////////
void flgr_static_fifo_read(FLGR_StaticFifo *fifo, int *row, int *col) {
  int rd=(fifo->rdptr);

  FLGR_DECLARE_FUNCTION;
  
  *row=fifo->row[rd];
  *col=fifo->col[rd];
  fifo->rdptr=(fifo->rdptr+1) % fifo->size;
}

//////////////////////////////////////////////////////////////////
/*! test if the fifo is empty
 *  @param fifo : Pointer to the fifo
 *  @returns FLGR_TRUE if fifo is empty
 */
//////////////////////////////////////////////////////////////////
int flgr_static_fifo_is_empty(FLGR_StaticFifo *fifo) {
  FLGR_DECLARE_FUNCTION;  
  return ((fifo->rdptr==fifo->wrptr)==FLGR_TRUE);
}

//////////////////////////////////////////////////////////////////
/*! return the filling level of the fifo
 *  @param fifo : Pointer to the fifo
 *  @returns filling level
 */
//////////////////////////////////////////////////////////////////
int flgr_static_fifo_level(FLGR_StaticFifo *fifo) {
  FLGR_DECLARE_FUNCTION;
  return (fifo->wrptr-fifo->rdptr);
}

//////////////////////////////////////////////////////////////////
/*! Destroy the fifo
 *  @param fifo : Pointer to the fifo
 *  @returns nothing
 */
//////////////////////////////////////////////////////////////////
void flgr_static_fifo_destroy(FLGR_StaticFifo *fifo) {
  FLGR_DECLARE_FUNCTION;  
  free(fifo->row);
  free(fifo->col);
  free(fifo);
}


//! @}
